<!-- eslint-disable vue/no-template-shadow -->
<!--
 * @Description: 告警处理模板
-->
<template>
  <BaseMainSlot>
    <template v-slot:btn-box>
      <ActionBar
        :page-no="103"
        :add-active="addActive"
        :del-active="delActive"
        :edit-active="editActive"
        :has-edit="false"
        :has-save="false"
        :cancel-active="cancelActive"
        :save-active="saveActive"
        @add="add"
        @cancel="cancel('formData')"
        @edit="edit"
        @del="del"
        @save="save('formData')"
      />
    </template>
    <template v-slot:search-box>
      <SearchBox @search="search">
        <div class="common-search-item">
          <div class="label">选择模板分类:</div>
          <div class="input">
            <el-select v-model="searchClassify" clearable>
              <el-option
                v-for="(item, index) in templateTypeList"
                :key="index"
                :value="item"
                :label="item"
              />
            </el-select>
          </div>
        </div>
      </SearchBox>
    </template>
    <template v-slot:table-box>
      <el-table
        ref="tableData"
        v-loading="loading"
        highlight-current-row
        :data="tableData"
        stripe
        border
        height="calc(100% - 50px)"
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          type="selection"
          width="55"
          :selectable="(el) => el.account"
        />
        <el-table-column type="index" label="序号" width="50" />
        <el-table-column prop="account" label="设置人账号" min-width="120" />
        <el-table-column prop="url" label="告警分类" />
        <el-table-column prop="type" label="模板类型" />
        <el-table-column prop="content" label="模板内容">
          <template v-if="scope.row.content" slot-scope="scope">
            <el-button
              size="small"
              type="success"
              plain
              @click="getInformation(scope.row)"
              >查看详情</el-button
            >
          </template>
        </el-table-column>
        <el-table-column width="200" label="操作">
          <template v-if="scope.row.content" slot-scope="scope">
            <el-button
              class="text-btn-cursor"
              type="text"
              size="small"
              @click="edit(scope.row)"
              ><i class="el-icon-edit-outline"></i> 编辑</el-button
            >
            <el-button
              class="text-btn-cursor cursor-delete"
              type="text"
              size="small"
              @click="deleteTemplate(scope.row)"
              ><i class="el-icon-delete"></i> 删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <Pagination
        :page-size="pageSize"
        :current-page="currentPage"
        :total-size="totalSize"
        @changePageSize="changePageSize"
        @changeCurrentPage="changeCurrentPage"
      />
    </template>
    <template v-slot:dialog>
      <el-dialog
        :title="titleText + '模板'"
        :visible.sync="dialogFormVisible"
        @close="cancel"
      >
        <el-form
          ref="formData"
          label-position="left"
          :model="formData"
          :rules="rules"
          class="el-form"
        >
          <el-form-item
            label="表单项选择:"
            prop="templateClassify"
            class="w100p"
          >
            <el-select
              v-model="formData.templateClassify"
              placeholder="请选择表单项"
              clearable
              @change="selectTemplateClassify"
            >
              <el-option
                v-for="(item, index) in templateClassifyList"
                :key="index"
                :label="item"
                :value="item"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="模板类型选择:" prop="templateType" class="w100p">
            <el-select
              v-model="formData.templateType"
              placeholder="请选择模板类型"
              filterable
              clearable
              @change="selectTemplateType"
            >
              <el-option
                v-for="(item, index) in templateTypeList"
                :key="index"
                :label="item"
                :value="item"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="模板内容:" prop="templateContent" class="w100p">
            <el-input
              v-model="formData.templateContent"
              type="textarea"
              placeholder="请输入模板内容"
              clearable
              @change="change($event)"
            />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button class="zh-button-one" @click="cancel"> 取 消 </el-button>
          <el-button
            class="zh-button-two"
            type="primary"
            @click="save('formData')"
          >
            保 存
          </el-button>
        </div>
      </el-dialog>
      <el-dialog title="模板内容详情" :visible.sync="dialogFormVisible1">
        {{ dialogContent }}
      </el-dialog>
    </template>
  </BaseMainSlot>
</template>
  
  <script>
import templateAPI from "@/api/basic-info-manage/template-Management/index";
// TODO:
import ActionBar from "@/components/ActionBar";
import Pagination from "@/components/Pagination";
import SearchBox from "@/components/SearchBox";
import BaseMainSlot from "@/components/BaseMainSlot/newBaseMainSlot.vue";
import { mymixin } from "@/utils/mixins";
import { mapGetters } from "vuex";

export default {
  // eslint-disable-next-line vue/name-property-casing
  name: "dataItemManagement",
  components: { Pagination, BaseMainSlot, SearchBox, ActionBar },
  mixins: [mymixin],
  data() {
    return {
      idList: [],
      searchClassify: "",
      totalSize: 0,
      pageSize: 10,
      currentPage: 1,
      templateList: [],
      // 模板类型
      templateTypeList: [],
      formData: {
        templateClassify: "",
        content: "",
        templateType: "",
        templateContent: ''
      },
      dialogFormVisible: false,
      dialogFormVisible1: false,
      templateClassifyList: [],
      processContentId: "",
      dialogContent: "",
      rules: {
        templateClassify: [
          {
            required: true,
            message: "表单项不能为空",
            trigger: "blur, change",
          },
        ],
        templateType: [
          {
            required: true,
            message: "表单类型不能为空",
            trigger: "blur, change",
          },
        ],
        templateContent: [
          {
            required: true,
            message: "模板内容不能为空",
            trigger: "blur, change",
          },
        ],
      },
    };
  },
  computed: {
    ...mapGetters({
      judgePermission: "permission/judgePermission",
    }),
  },
  watch: {},
  created() {
    this.getTableDataList();
    this.getTemplateType();
  },
  methods: {
    change() {
      this.$forceUpdate()
    },
    // TODO:
    // 获取表格数据
    async getTableDataList(formName = "") {
      let params = {
        page: this.currentPage,
        size: this.pageSize,
        entity: {
          type: formName,
        },
      };
      let { data: res } = await templateAPI.queryConditionalPage(params);
      if (res.content.length > 0 || this.currentPage==0) {
        this.loading = false;
        this.tableData = res.content;
        this.totalSize = res.totalElements;
        this.addblankmet();
      } else {
        if(this.currentPage==0)
        return
        this.currentPage = this.currentPage - 1;
        if(this.searchClassify) {
          this.getTableDataList(this.searchClassify);
        } else {
          this.getTableDataList();
        }
      }
    },
    // 查询
    search() {
      this.currentPage = 1;
      this.getTableDataList(this.searchClassify);
    },
    // 获取模板类型
    async getTemplateType() {
      let res = await templateAPI.queyTemplateType();
      this.templateTypeList = res.data;
      // 分类
      let ress = await templateAPI.queryClassify();
      this.templateClassifyList = ress.data;
    },
    // 选择模板类型
    selectTemplateType(e) {
      this.formData.templateType = e;
    },
    selectTemplateClassify(e) {
      this.formData.templateClassify = e;
      console.log(this.formData.templateClassify);
    },
    changePageSize(pageSize) {
      this.pageSize = pageSize;
      this.getTableDataList();
    },
    changeCurrentPage(currentPage) {
      this.currentPage = currentPage;
      this.getTableDataList();
    },
    // 删除--TODO:有问题
    deleteTemplate(row) {
      this.$confirm("你确定要删除此模板吗？", "确认信息", {
        distinguishCancelAndClose: true,
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      })
        .then(async () => {
          let res = await templateAPI.deleteTemplate(row.processContentId);
          if (res.code === 1) {
            this.$message.success("删除成功！");
            this.getTableDataList();
          }
          this.$message({
            type: "success",
            message: "删除成功",
          });
        })
        .catch((action) => {
          this.$message({
            type: "info",
            message: "你已取消删除！",
          });
        });
    },
    // 修改
    edit(row) {
      this.nowChanceStau = "edit";
      this.titleText = "编辑";
      this.formData.templateType = row.type;
      this.formData.templateClassify = row.formName;
      this.formData.templateContent = row.content;
      this.processContentId = row.processContentId;
      this.dialogFormVisible = true;
    },
    // TODO:
    handleSelectionChange(list) {
      this.idList = [];
      this.idList = list.map((item) => item.processContentId);
      if (this.idList.length > 0) this.delActive = true;
      else this.delActive = false;
    },
    save() {
      this.$refs.formData.validate(async (valid) => {
        if (valid) {
          let params = {
            content: this.formData.templateContent,
            type: this.formData.templateType,
            formName: this.formData.templateClassify,
            account: sessionStorage.getItem("account"),
            organizationNo: sessionStorage.getItem("organizationNo"),
          };
          if (this.nowChanceStau === "edit") {
            // 编辑接口
            params["processContentId"] = this.processContentId;
            let res = await templateAPI.editTemplate(params);
            if (res.code === 1) {
              this.$message.success("修改模板成功！");
              this.getTableDataList();
              this.dialogFormVisible = false;
            } else {
              this.$message.error("修改模板失败！");
            }
            this.$refs.formData.resetFields();
          } else if (this.nowChanceStau === "add") {
            let res = await templateAPI.setTemplate(params);
            if (res.code === 1) {
              this.$message.success("添加模板成功！");
              this.getTableDataList();
              this.dialogFormVisible = false;
            } else {
              this.$message.error(res.msg);
            }
            this.$refs.formData.resetFields();
          }
        } else {
          return false;
        }
      });
    },
    del() {
      this.$confirm("你确定要删除这些模板吗？", "确认信息", {
        distinguishCancelAndClose: true,
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      })
        .then(async () => {
          let res = await templateAPI.batchDeleteTemplate(this.idList);
          if (res.code === 1) {
            this.$message.success("删除成功！");
            this.getTableDataList();
          }
          this.$refs.tableData.clearSelection();
          this.$message({
            type: "success",
            message: "删除成功",
          });
        })
        .catch((action) => {
          this.$refs.tableData.clearSelection();
          this.$message({
            type: "info",
            message: "你已取消删除！",
          });
        });
    },
    // 查看内容详情
    getInformation(row) {
      this.dialogFormVisible1 = true;
      this.dialogContent = row.content;
    },
    add() {
      this.canInput = true;
      this.nowChanceStau = "add";
      this.formData = {};
      this.List = {};
      this.image = null;
      this.tagsList = [];
      this.unitTypeId = "";
      this.unitsList = [];
      this.basicUrl = this.reseUrl;
      this.postName = "";
      this.InImgUrl = "";
      this.titleText = "添加";
      this.dialogFormVisible = true;
      this.addActive = false;
      this.delActive = false;
      this.saveActive = true;
      this.cancelActive = true;
      this.editActive = false;
      // this.$nextTick(()=>{
      //   this.$refs.formData.resetFields()
      // })
    },
    cancel() {
      this.$nextTick(() => {
        this.$refs.formData.resetFields();
      });
      this.dialogFormVisible = false;
      this.addActive = true;
    },
  },
};
</script>
  
  <style scoped lang="scss">
@import "~@/styles/variables.scss";
::v-deep .el-dialog {
  .el-dialog__body {
    overflow-y: auto;
  }
  .el-input__inner {
    // background-color: rgba($color: #000000, $alpha: 0) !important;
    border: none !important;
  }
}
::v-deep .common-layout {
  width: calc(100% - 1px) !important;
  .zhcontent {
    position: relative;
    height: calc(100% - 100px) !important;
    .pagination {
      right: 0px !important;
    }
  }
}
::v-deep .el-table_1_column_6 {
  // height: 80px;
}
</style>
  